package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.constants.WmNewsMessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@Component
@Slf4j
public class ArticleIsDownListener {

    @Autowired
    ApArticleConfigService apArticleConfigService;

/**
 * wemedia从kafka接收上下架状态
 * */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMessage(String message) {
        //JSON是用message接收的，所以判断是否为空
        if (StringUtils.isNotBlank(message)) {
            log.info("ArticleIsDownListener receiveMessage:{}",message);
            //把生产的map拿出来
            Map map = JSON.parseObject(message, Map.class);
            // 更新文章的配置信息
            // 这个地方我觉得不太严谨
            boolean isDown = true; // 下架
            //用isDown来自动判断是否上下架
            if (map.get("enable").equals(1)) {
                //如果是上架，那么就再改回来
                isDown = false;
            }
            //更新文章配置信息状态
            //APP已发布文章配置表
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId,map.get("articleId") )
                    .set(ApArticleConfig::getIsDown, isDown)
            );


            log.info("update apArticleConfig is success...");

        }


    }

}